﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
*/
// JScript File
var btnConfirm;
var chbRate;
var rbtArray;
var hfArray;
var lblArray;
var UECountries;
var baseCountry;
var lblNoShipmentAvailable;
var isRateSelected = false;

var DELIMETER = '$';
var BLOCKED = 'X';
var ACTIVE = 'A';
var DISACTIVE = 'D';

function ShowPaymentForm(obj)
{
    PaymentFormVisibility(obj.value)
}

function ChbRateClick(obj)
{
    btnConfirm.disabled = !(obj.checked);
}

function PaymentFormVisibility(value)
{
    btnConfirm.disabled = false;
    if(value == 0)
    {
        isRateSelected = false;
        window.document.getElementById("online-payment").style.display = '';
        window.document.getElementById("rate-payment").style.display = 'none';
    }
    else if(value == 3)
    {
        btnConfirm.disabled = !(chbRate.checked);
        isRateSelected = true;
        window.document.getElementById("rate-payment").style.display = '';
        window.document.getElementById("online-payment").style.display = 'none';
    }
    else
    {
        isRateSelected = false;
        window.document.getElementById("rate-payment").style.display = 'none';
        window.document.getElementById("online-payment").style.display = 'none';
    }
}


function SetElementOnLoad(btnConfirmName, chbRateName) 
{
    btnConfirm = window.document.getElementById(btnConfirmName);
    chbRate = window.document.getElementById(chbRateName);
}

function SetShipment(activeArray, ddlName)
{
    ddl = window.document.getElementById(ddlName);
    setVisible = false;
    isFirst = false;
    shipmentPriceIndex = GetShipmentPriceIndex(ddl);
    
    for (var i = 0; i < rbtArray.length; i++)
    {
        value = rbtArray[i].getAttribute('value');
        for (var j = 0; j < activeArray.length; j++)
        {
            //jeśli wartość się zgadza i rodzaj wysyłki nie jest zablokowany to go pokazujemy
            if (value == activeArray[j])
            {
                if (hfArray[i].value.split(DELIMETER)[shipmentPriceIndex] != BLOCKED)
                {
                    rbtArray[i].style.display = '';
                    rbtArray[i].blocked = ACTIVE;
                    if (!isFirst)
                    {
                        isFirst = true;
                        CheckRadioButton(rbtArray[i], true);
                    }
                    setVisible = true;
                    break;
                }
                else
                {
                    rbtArray[i].blocked = BLOCKED;
                    rbtArray[i].style.display = 'none';
                    setVisible = true;
                }
            }
        }

        if (!setVisible)
        {
            rbtArray[i].style.display = 'none';
            rbtArray[i].blocked = DISACTIVE;
        }
        setVisible = false;
    }

    CheckFirstActiveRadio();
}

function CheckRadioButton(div, isChecked)
{
    radio = GetRadioButtonFromPanel(div);
    radio.checked = isChecked;
}

function GetRadioButtonFromPanel(div)
{
    radio = div.childNodes[0];
    var i = 1;
    while (i <= div.childNodes.length && radio.nodeName.toUpperCase() != 'INPUT')
    {
        radio = div.childNodes[i];
        i++;
    }

    return radio;
}

function SetShipmentPrice(obj)
{
    shipmentPriceIndex = GetShipmentPriceIndex(obj);
    
    
    for (i = 0; i < lblArray.length; i++)
    {
        price = hfArray[i].value.split(DELIMETER)[shipmentPriceIndex];
        if(price == BLOCKED && rbtArray[i].blocked != DISACTIVE)
        {
            rbtArray[i].style.display = 'none';
            rbtArray[i].blocked = BLOCKED;
        }
        else
        {
            lblArray[i].innerHTML = price;
            if (rbtArray[i].blocked == BLOCKED)
            {
                rbtArray[i].style.display = '';
                rbtArray[i].blocked = ACTIVE;
            }
        }
    }

    CheckFirstActiveRadio();
}

function SetDefaultShipmentPrice(obj, ddl, value)
{
    if (!(obj.checked))
    {
        ddl.value = value;
        SetShipmentPrice(ddl);
    }
}

function GetShipmentPriceIndex(obj)
{
    shipmentPriceIndex = 2;

    if (baseCountry == obj.value)
    {
        shipmentPriceIndex = 0;
    }
    else
    {
        for (i = 0; i < UECountries.length; i++)
        {
            if (UECountries[i] == obj.value)
            {
                shipmentPriceIndex = 1;
                break;
            }
        }
    }

    return shipmentPriceIndex;
}

function CheckFirstActiveRadio()
{
    oneChecked = false;
    for (i = 0; i < rbtArray.length; i++)
    {
        if (!(oneChecked) && rbtArray[i].blocked == ACTIVE)
        {
            oneChecked = true;
            CheckRadioButton(rbtArray[i], true);
        }
        else
        {
            CheckRadioButton(rbtArray[i], false);
        }
    }

    if (oneChecked)
    {
        lblNoShipmentAvailable.style.display = 'none';
        if (!(isRateSelected && !(chbRate.checked)))
        {
            btnConfirm.disabled = false;
        }
    }
    else
    {
        btnConfirm.disabled = true;
        lblNoShipmentAvailable.style.display = '';
    }
}

function ShowInfoIfNeed()
{
    for (i = 0; i < rbtArray.length; i++)
    {
        if (GetRadioButtonFromPanel(rbtArray[i]).checked)
        {
            lblNoShipmentAvailable.style.display = 'none';
            return;
        }
    }

    lblNoShipmentAvailable.style.display = '';
}


function SetUniqueRadioButton(nameregex, current)
{
    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++)
    {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio')
        {
            if (re.test(elm.name))
            {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}




